//
//  Utils.m
//  SoccerLottery
//
//  Created by chuanshuangzhang chuan shuang on 15/12/24.
//  Copyright © 2015年 chuanshuangzhang. All rights reserved.
//

#import "Utils.h"

#import "UIImageView+WebCache.h"

@implementation Utils
+ (void)ShowTimeDownWithTime:(NSInteger)totalTime block:(TimeCallBackBlock)block
{
    __block NSInteger time = totalTime;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        time = time - 1;
        dispatch_async(dispatch_get_main_queue(), ^{
            block(_timer,NO,time);
        });
        if(time==0){
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                block(_timer,YES,0);
            });
        }
    });
    dispatch_resume(_timer);
}
+(id)instantiateViewControllerWithIdentifier:(NSString *)identifier
{
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    return [story instantiateViewControllerWithIdentifier:identifier];
}

+ (UIImage *)createImageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}
+ (UIViewController *)activityViewController
{
    UIViewController* activityViewController = nil;
    
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    if(window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow *tmpWin in windows)
        {
            if(tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    NSArray *viewsArray = [window subviews];
    if([viewsArray count] > 0)
    {
        UIView *frontView = [viewsArray objectAtIndex:0];
        
        id nextResponder = [frontView nextResponder];
        
        if([nextResponder isKindOfClass:[UIViewController class]])
        {
            activityViewController = nextResponder;
        }
        else
        {
            activityViewController = window.rootViewController;
        }
    }
    
    return activityViewController;
}

+(void )loadImage:(UIImageView *)imageView imageUrl:(NSString *)url
{
    [imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"defaultImage"]];
}
+(NSMutableAttributedString *)setAttributeString:(NSString *)str1 withString:(NSString *)str2
{
    CGFloat fontSize = 15.0;
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",str1,str2]];
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:15] range:NSMakeRange(0, str1.length)];
    [str addAttribute:NSForegroundColorAttributeName value:RGBACOLOR(83, 83, 83, 1.0) range:NSMakeRange(0, str1.length)];
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:fontSize] range:NSMakeRange(str.length - str2.length, str2.length)];
    [str addAttribute:NSForegroundColorAttributeName value:RGB(42, 60, 151, 1) range:NSMakeRange(str.length - str2.length, str2.length)];
    return str;
}

+ (void)loadImage:(UIImageView *)imageView imageUrl:(NSString *)url defaultImage:(NSString *)imageName
{
//    [imageView sd_setImageWithURL:[NSURL URLWithString:PICURL(url)] placeholderImage:[UIImage imageNamed:imageName]];
}

+(NSString *)transferShortTime:(NSString *)timeStr
{
    if(!isValidString(timeStr)){
        return @"";
    }
    NSInteger t = [timeStr integerValue];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSDate  *date = [NSDate dateWithTimeIntervalSince1970:t];
    NSTimeZone *zone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    [formatter setDateFormat:@"MM-dd"];
    NSString *confromTimespStr = [formatter stringFromDate:localeDate];
    return confromTimespStr;
}
+(NSString *)transferFullTime:(NSString *)timeStr
{
    if(!isValidString(timeStr)){
        return @"";
    }
    NSInteger t = [timeStr integerValue];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSDate  *date = [NSDate dateWithTimeIntervalSince1970:t];
    NSTimeZone *zone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    [formatter setDateFormat:@"YYYY-MM-dd-HH:mm"];
    NSString *confromTimespStr = [formatter stringFromDate:localeDate];
    return confromTimespStr;
}
+(NSInteger)timeIntervalFromDate:(NSDate *)fromDate toDate:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    long dd = (long)[date timeIntervalSince1970] - [fromDate timeIntervalSince1970];
    return dd/60;
}
@end
